package com.example.administrator.caigou51.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.ab.util.AbLogUtil;
import com.ab.util.AbViewUtil;
import com.alibaba.fastjson.JSON;
import com.example.administrator.caigou51.EventBusClass.UpdateGoodsDetail;
import com.example.administrator.caigou51.EventBusClass.UpdateShopCarRedCount;
import com.example.administrator.caigou51.R;
import com.example.administrator.caigou51.activity.GBaseActivity;
import com.example.administrator.caigou51.activity.MainActivity;
import com.example.administrator.caigou51.bean.GoodsDetailBean;
import com.example.administrator.caigou51.bean.MainDataBean;
import com.example.administrator.caigou51.control.DialogGoodsBuy;
import com.example.administrator.caigou51.global.App;
import com.example.administrator.caigou51.global.Constant;
import com.example.administrator.caigou51.recyclerCard.basic.MaterialListView;
import com.example.administrator.caigou51.recyclerCard.card.GoodsDetailCard;
import com.example.administrator.caigou51.request.BaseResponse;
import com.example.administrator.caigou51.request.ICallBackForRequestServer;
import com.example.administrator.caigou51.request.RequestServerManager;
import com.example.administrator.caigou51.util.AnimUtils;

import de.greenrobot.event.EventBus;


/**
 * Created by cjy on 2015/10/3.
 */
@SuppressLint("ValidFragment")
public class GoodsInfoFragment extends BaseFragment implements View.OnClickListener {
    private View holderView;
    private MaterialListView material_listview;
    private boolean isScaled = false;
    private String itemID;
    GoodsDetailBean goodsDetailBean;

    float x1 = 0;
    float x2 = 0;
    float y1 = 0;
    float y2 = 0;
    private boolean isCanTouchMove=false;

    public GoodsInfoFragment(String stringExtra) {
        super();
        itemID=stringExtra;
    }
    public GoodsInfoFragment() {

    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (holderView == null) {
            holderView = inflater.inflate(R.layout.fragment_goods_info, container, false);
            material_listview= (MaterialListView) holderView.findViewById(R.id.material_listview);

            material_listview= (MaterialListView) holderView.findViewById(R.id.material_listview);
            initDatas();
            TaskGetGoodsDetail();
        }
        ViewGroup parent = (ViewGroup) holderView.getParent();
        if (parent != null) {
            parent.removeView(holderView);
        }
        if (!isScaled) {
            AbViewUtil.scaleContentView((ViewGroup) holderView);
            isScaled = true;
        }

        return holderView;
    }


    private void TaskGetGoodsDetail() {
        RequestServerManager.getInstanceDIY().handleMethodDIY((GBaseActivity) getActivity(), material_listview, false, null,
                Constant.getRootUrl() + Constant.Action.Action_GoodsDetail, Constant.Action.Action_GoodsDetail, RequestServerManager.getInstanceDIY().
                        getRequestParams(Constant.Action.Action_GoodsDetail,itemID), BaseResponse.class, new ICallBackForRequestServer<BaseResponse>() {
                    @Override
                    public void onSuccess(BaseResponse result) {
                        // JSONObject jsonObject = (JSONObject) result.getData();
                        goodsDetailBean  = JSON.parseObject(result.getData().toString(), GoodsDetailBean.class);
                        EventBus.getDefault().post(new UpdateGoodsDetail(goodsDetailBean));
                        fillArrayInListView(goodsDetailBean);

                    }

                    @Override
                    public void onFail(int type, String errorMsg, String json) {

                        Intent intent=new Intent(getActivity(), MainActivity.class);
                        startActivity(intent);
                    }
                });
    }
    protected void initDatas() {

    }

    protected void fillArrayInListView(GoodsDetailBean bean) {
        GoodsDetailCard goodsDetailCard=new GoodsDetailCard(getActivity());
        goodsDetailCard.setGoodsDetailBean(bean);
        goodsDetailCard.setMaterialListviewMain(material_listview);


        material_listview.add(goodsDetailCard);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {

        }
    }
    public void onEventMainThread(UpdateShopCarRedCount updateShopCarRedCount) {

        material_listview.getAdapter().notifyDataSetChanged();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
